package com.suneast.suneaststore.modules.goodsdetail;

import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.Toolbar;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.suneast.suneaststore.App;
import com.suneast.suneaststore.R;
import com.suneast.suneaststore.app.AccountManager;
import com.suneast.suneaststore.bean.goodsdetail.GoodsDetailBean;
import com.suneast.suneaststore.bean.personal.address.AddressBean;
import com.suneast.suneaststore.bean.personal.trace.TraceBean;
import com.suneast.suneaststore.bean.personal.trace.TraceBean_;
import com.suneast.suneaststore.bean.personal.trace.TraceDateBean;
import com.suneast.suneaststore.bean.personal.trace.TraceDateBean_;
import com.suneast.suneaststore.modules.common.Flag;
import com.suneast.suneaststore.modules.common.GlideImageLoader;
import com.suneast.suneaststore.modules.goodsdetail.event.AddGoodsToCartResult;
import com.suneast.suneaststore.modules.goodsdetail.event.CollectGoodsResult;
import com.suneast.suneaststore.modules.goodsdetail.event.ConfirmGoodsEvent;
import com.suneast.suneaststore.modules.goodsdetail.event.GetDetailEvent;
import com.suneast.suneaststore.modules.goodsdetail.event.GotGoodsCollectState;
import com.suneast.suneaststore.modules.goodsdetail.event.GotSpecsDataEvent;
import com.suneast.suneaststore.modules.goodsdetail.event.SubCateLoaded;
import com.suneast.suneaststore.modules.goodsdetail.subfragment.GoodsIntroFragment;
import com.suneast.suneaststore.modules.goodsdetail.subfragment.GoodsPackageFragment;
import com.suneast.suneaststore.modules.goodsdetail.subfragment.GoodsRecommendFragment;
import com.suneast.suneaststore.modules.goodsdetail.subfragment.GoodsSpecsListFragment;
import com.suneast.suneaststore.modules.msg.chat.ChatFragment;
import com.suneast.suneaststore.modules.personal.address.AddressListFragment;
import com.suneast.suneaststore.modules.personal.sign.SignInFragment;
import com.suneast.suneaststore.modules.shopcart.BillConfirmFragment;
import com.suneast.suneaststore.modules.shopcart.ShopCartFragment;
import com.suneast.suneaststore.modules.shopcart.ShopCartHandler;
import com.suneast.suneaststore.modules.store.StoreFragment;
import com.suneast.suneaststore.ui.banner.Banner;
import com.suneast.suneaststore.ui.banner.BannerConfig;
import com.suneast.suneaststore.ui.customview.NormalItemView;
import com.suneast.suneaststore.ui.customview.TitleView;
import com.suneast.suneaststore.ui.fragments.BaseFragment;
import com.suneast.suneaststore.util.dimen.ScreenUtil;
import com.suneast.suneaststore.util.log.LatteLogger;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.joda.time.DateTime;

import butterknife.BindView;
import butterknife.OnClick;
import io.objectbox.Box;
import io.objectbox.query.QueryBuilder;
import me.majiajie.pagerbottomtabstrip.NavigationController;
import me.majiajie.pagerbottomtabstrip.PageNavigationView;
import me.majiajie.pagerbottomtabstrip.listener.OnTabItemSelectedListener;


public class GoodsDetailFragment extends BaseFragment {

    private final static int FAR_AWAY_DATE = 21000000;
    private final static long FAR_AWAY_TIME = 21000000000000L;

    private int merchantId = -1;

    @Override
    public @LayoutRes
    int setLayout() {
        return R.layout.fragment_goods_detail;
    }

    public GoodsDetailFragment withId(int id) {
        this.goodsId = id;
        return this;
    }

    public GoodsDetailFragment withMerchId(int merchantId) {
        this.merchantId = merchantId;
        return this;
    }

    @Override
    public void onFragmentResult(int requestCode, int resultCode, Bundle data) {
        super.onFragmentResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 3: {
                if (resultCode == RESULT_OK) {
                    AddressBean addressBean = data.getParcelable("address_bean");
                    addressTv.setText(addressBean.getProvince() + addressBean.getCity()
                            + addressBean.getArea());
                }
            }
            break;
        }
    }

    private int goodsId = 1252;
    private Box<TraceDateBean> dateBeanBox;
    private Box<TraceBean> traceBeanBox;

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        EventBus.getDefault().register(this);
        dateBeanBox = App.getBoxStore().boxFor(TraceDateBean.class);
        traceBeanBox = App.getBoxStore().boxFor(TraceBean.class);
        initView();
        if (merchantId != -1) {
            GoodsHandler.requestDetailInStore(goodsId, merchantId);
        } else {
            GoodsHandler.requestDetail(goodsId);
        }
    }

    @Override
    public void onDestroyView() {
        goodsDetailBanner.stopAutoPlay();
        EventBus.getDefault().unregister(this);
        if (dialog != null) {
            dialog.cancel();
        }
        super.onDestroyView();
    }

    @OnClick({
            R.id.img_collect,
            R.id.btn_chat,
            R.id.btn_add_to_cart,
            R.id.btn_buy_it_now,
            R.id.btn_enter_store,
            R.id.tv_specification_value,
            R.id.img_more_specification,
            R.id.tv_address,
            R.id.img_more_address,
            R.id.tv_more_comment
    })
    public void onViewClick(View v) {
        switch (v.getId()) {
            case R.id.img_collect: {
                //加入收藏或取消收藏
                if (AccountManager.getSignState()) {
                    GoodsHandler.collectGoodsOrNot(goodsId, collectState + 1);
                } else {
                    Toast.makeText(_mActivity, "请登录后使用收藏功能", Toast.LENGTH_SHORT).show();
                }
            }
            break;
            case R.id.btn_chat: {
                if (AccountManager.getSignState()) {
                    // 打开客服聊天界面
                    ChatFragment delegate = new ChatFragment();
                    Bundle bundle = new Bundle();
                    bundle.putInt("store_id", mDetailBean.getMerchId());
                    delegate.setArguments(bundle);
                    getSupportDelegate().start(delegate);
                } else {
                    startSignInFragment();
                }
            }
            break;
            case R.id.btn_add_to_cart: {
                if (AccountManager.getSignState()) {
                    // 弹出规格选择界面
                    showSpecDialog(0);
                } else {
                    startSignInFragment();
                }
            }
            break;
            case R.id.btn_buy_it_now: {
                if (AccountManager.getSignState()) {
                    //弹出规格选择界面
                    showSpecDialog(1);
                } else {
                    startSignInFragment();
                }
            }
            break;
            case R.id.btn_enter_store: {
                // 打开商店界面
                StoreFragment storeDelegate = new StoreFragment().withId(mDetailBean.getMerchId());
                start(storeDelegate);
            }
            break;
            case R.id.tv_specification_value:
            case R.id.img_more_specification: {
                if (AccountManager.getSignState()) {
                    showSpecDialog(2);
                } else {
                    startSignInFragment();
                }
            }
            break;
            case R.id.tv_address:
            case R.id.img_more_address: {
                if (AccountManager.getSignState()) {
                    AddressListFragment fragment = new AddressListFragment();
                    Bundle bundle = new Bundle();
                    bundle.putInt(AddressListFragment.KEY_TYPE, 1);
                    fragment.setArguments(bundle);
                    getSupportDelegate().startForResult(fragment, 3);
                } else {
                    startSignInFragment();
                }
            }
            break;
            case R.id.tv_more_comment: {

            }
            break;
        }
    }

    private void startSignInFragment() {
        start(new SignInFragment());
    }

    @BindView(R.id.toolbar_goods_detail)
    Toolbar goodsDetailToolBar;

    @BindView(R.id.nsv_goods_detail)
    NestedScrollView nsvGoodsDetail;

    @BindView(R.id.banner_goods_detail)
    Banner goodsDetailBanner;

    @BindView(R.id.layout_comment)
    FrameLayout commentFrame;

    @BindView(R.id.navi_shop_cart)
    PageNavigationView bottomNaviTab;
    private NavigationController mTabController;

    @BindView(R.id.rg_goods_detail)
    RadioGroup goodsDetailRg;
    @BindView(R.id.rb_goods_intro)
    RadioButton goodsIntroRb;
    @BindView(R.id.rb_specification)
    RadioButton specificationRb;
    @BindView(R.id.rb_packaging)
    RadioButton packagingRb;
    @BindView(R.id.rb_recommend)
    RadioButton recommendedRb;

    @BindView(R.id.title_view_detail)
    TitleView bottomTitleView;

    @BindView(R.id.frame_detail_container)
    FrameLayout bottomFrameLayout;

    private Resources mResources;

    private int commentScrollHeight = 0;
    private int detailScrollHeight = 0;

    private void initView() {
        commentScrollHeight = ScreenUtil.dip2px(_mActivity, 575);
        detailScrollHeight = ScreenUtil.dip2px(_mActivity, 655);
        goodsDetailToolBar.setNavigationOnClickListener((v) -> pop());
        mResources = getResources();
        topTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                setNestedScrollView(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                setNestedScrollView(tab.getPosition());
            }
        });
        commentFrame.getMeasuredHeight();
        goodsDetailBanner.setImageLoader(new GlideImageLoader());
        goodsDetailBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        goodsDetailBanner.isAutoPlay(true);
        goodsDetailBanner.setDelayTime(5000);
        goodsDetailBanner.setIndicatorGravity(BannerConfig.BOTTOM_CENTER);

        NormalItemView normalItemView = new NormalItemView(getContext());
        normalItemView.initialize(R.drawable.selector_btn_shop_cart,
                R.drawable.selector_btn_shop_cart, "购物车");
        normalItemView.setTextDefaultColor(mResources.getColor(R.color.black_light));
        normalItemView.setTextCheckedColor(mResources.getColor(R.color.black_light));
        mTabController = bottomNaviTab.custom()
                .addItem(normalItemView)
                .build();

        mTabController.addTabItemSelectedListener(new OnTabItemSelectedListener() {
            @Override
            public void onSelected(int index, int old) {
                ShopCartFragment fragment = new ShopCartFragment();
                Bundle bundle = new Bundle();
                bundle.putInt("type", 1);
                fragment.setArguments(bundle);
                start(fragment);
            }

            @Override
            public void onRepeat(int index) {
                ShopCartFragment fragment = new ShopCartFragment();
                Bundle bundle = new Bundle();
                bundle.putInt("type", 1);
                fragment.setArguments(bundle);
                start(fragment);
            }
        });

        goodsDetailRg.setOnCheckedChangeListener((RadioGroup group, int checkedId) -> {
            switch (checkedId) {
                case R.id.rb_goods_intro: {
                    bottomTitleView.setTitleName("详情图");
                    changePage(0, goodsId);
                }
                break;
                case R.id.rb_specification: {
                    bottomTitleView.setTitleName("规格参数");
                    changePage(1, goodsId);
                }
                break;
                case R.id.rb_packaging: {
                    bottomTitleView.setTitleName("售后说明");
                    changePage(2, goodsId);
                }
                break;
                case R.id.rb_recommend: {
                    bottomTitleView.setTitleName("同店推荐");
                    changePage(3, goodsId);
                }
                break;
            }
        });
    }

    private void setNestedScrollView(int pos) {
        switch (pos) {
            case 0: {
                nsvGoodsDetail.scrollTo(0, 0);
            }
            break;
            case 1: {
                nsvGoodsDetail.scrollTo(0, commentScrollHeight);
            }
            break;
            case 2: {
                nsvGoodsDetail.scrollTo(0, detailScrollHeight);
            }
            break;
        }
    }

    @BindView(R.id.tab_top_goods_detail)
    TabLayout topTab;

    @BindView(R.id.tv_discount_info)
    TextView discountInfoTv;

    @BindView(R.id.tv_product_name)
    TextView goodsNameTv;

    @BindView(R.id.tv_member_price)
    TextView memberPriceTv;

    @BindView(R.id.tv_discount_price)
    TextView discountPriceTv;

    @BindView(R.id.tv_specification_value)
    TextView specificationValueTv;

    @BindView(R.id.tv_address)
    TextView addressTv;

    @BindView(R.id.tv_freight_charge)
    TextView freightChargeTv;

    @BindView(R.id.tv_good_comment_rate)
    TextView goodCommentRateTv;

    @BindView(R.id.tv_more_comment)
    TextView moreCommentTv;

    @BindView(R.id.tv_no_comment_tip)
    TextView noCommentTv;

    @BindView(R.id.img_user_icon)
    ImageView userIconImg;

    @BindView(R.id.tv_user_name)
    TextView userNameTv;

    @BindView(R.id.tv_comment)
    TextView commentTv;

    @BindView(R.id.img_collect)
    ImageView collectImg;

    @BindView(R.id.tv_collect)
    TextView collectTv;

    GoodsDetailBean mDetailBean = new GoodsDetailBean();

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGotDetail(GetDetailEvent event) {
        GoodsHandler.getGoodsCollectState(goodsId);
        mDetailBean = event.getDetailBean();
        goodsDetailBanner.setImages(mDetailBean.getThumbList());
        goodsDetailBanner.start();
        discountInfoTv.setText(mDetailBean.getFavourable());
        goodsNameTv.setText(mDetailBean.getTitle());

        String mPrice = mDetailBean.getMemberPrice();
        String d2Price = mDetailBean.getD2Price();
        String d2Discount = mDetailBean.getD2Discount();
        if (mDetailBean.getType() == 1) {  //普通商城商品
            String unitStr = mDetailBean.getUnit();
            String memberPriceStr = String.format(mResources.getString(R.string.member_price_normal), mPrice, unitStr);
            String discountPriceStr = String.format(mResources.getString(R.string.discount_price_normal), d2Price, unitStr, d2Discount, unitStr);

            Spannable memberSpan = new SpannableString(memberPriceStr);
            memberSpan.setSpan(new AbsoluteSizeSpan(40), 0, 1 + mPrice.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            memberSpan.setSpan(new ForegroundColorSpan(Color.RED), 0, 1 + mPrice.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            memberPriceTv.setText(memberSpan);

            if (Double.valueOf(d2Discount) > 0) {
                discountPriceTv.setVisibility(View.VISIBLE);
                Spannable dSpan = new SpannableString(discountPriceStr);
                int secondStartPos = discountPriceStr.indexOf("+");
                dSpan.setSpan(new AbsoluteSizeSpan(40), 0, 1 + d2Price.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                dSpan.setSpan(new ForegroundColorSpan(Color.RED), 0, 1 + d2Price.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                dSpan.setSpan(new AbsoluteSizeSpan(40), secondStartPos, secondStartPos + d2Discount.length() + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                dSpan.setSpan(new ForegroundColorSpan(Color.RED), secondStartPos, secondStartPos + d2Discount.length() + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                discountPriceTv.setText(dSpan);
            } else {
                commentScrollHeight = ScreenUtil.dip2px(_mActivity, 540);
                detailScrollHeight = ScreenUtil.dip2px(_mActivity, 620);
                discountPriceTv.setVisibility(View.GONE);
            }
        } else {//兑换商城商品
            String memberPriceStr = String.format(mResources.getString(R.string.member_price_exchange), d2Discount, d2Price);
            Spannable spannable = new SpannableString(memberPriceStr);
            spannable.setSpan(new AbsoluteSizeSpan(40), 5, 6 + d2Discount.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannable.setSpan(new AbsoluteSizeSpan(40), 5 + d2Price.length() + 5, 5 + d2Discount.length() + 9 + mPrice.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannable.setSpan(new ForegroundColorSpan(Color.RED), 5, 5 + d2Price.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannable.setSpan(new ForegroundColorSpan(Color.RED), 5 + d2Price.length() + 5, 5 + d2Price.length() + 8 + mPrice.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            memberPriceTv.setText(spannable);

            String retailPriceStr = String.format(mResources.getString(R.string.retail_price_exchange), mPrice);
            Spannable retailSpan = new SpannableString(retailPriceStr);
            retailSpan.setSpan(new AbsoluteSizeSpan(40), 5, 5 + mPrice.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            retailSpan.setSpan(new ForegroundColorSpan(Color.RED), 5, 5 + mPrice.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            discountPriceTv.setText(retailSpan);
        }

        addressTv.setText(mDetailBean.getExpressAddress());
        if (mDetailBean.getFreight() != 0) {
            freightChargeTv.setText(String.format(mResources.getString(R.string.freight_charge),
                    "" + mDetailBean.getFreight()));
        }
        goodCommentRateTv.setText(String.format(mResources.getString(R.string.comment_rate), String.valueOf(mDetailBean.getGoodRate())));
        moreCommentTv.setText(String.format(mResources.getString(R.string.total_comment_count),
                "" + mDetailBean.getTotalComment()));
        if (mDetailBean.isNoComment()) {
            noCommentTv.setVisibility(View.VISIBLE);
            userIconImg.setVisibility(View.GONE);
            userNameTv.setVisibility(View.GONE);
            commentTv.setVisibility(View.GONE);
        } else {
            commentScrollHeight = ScreenUtil.dip2px(_mActivity, 625);
            detailScrollHeight = ScreenUtil.dip2px(_mActivity, 705);
            noCommentTv.setVisibility(View.GONE);
            userIconImg.setVisibility(View.VISIBLE);
            userNameTv.setVisibility(View.VISIBLE);
            commentTv.setVisibility(View.VISIBLE);
            Glide.with(_mActivity).load(mDetailBean.getLastCommentUserHeadImgUrl()).into(userIconImg);
            userNameTv.setText(mDetailBean.getLastCommentUserName());
            commentTv.setText(mDetailBean.getLastComment());
        }

        GoodsIntroFragment delegate = new GoodsIntroFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("id", goodsId);
        delegate.setArguments(bundle);
        getSupportDelegate().loadRootFragment(R.id.frame_detail_container, delegate);

        DateTime nowTime = DateTime.now();
        String dateStr = nowTime.toString("yyyy-MM-dd");
        String timeStr = nowTime.toString("yyyy-MM-dd-HH-mm-ss");
        QueryBuilder<TraceDateBean> queryBuilder = dateBeanBox.query();
        queryBuilder.equal(TraceDateBean_.dateStr, dateStr);

        TraceBean traceBean = new TraceBean(event.getDetailBean().getId(), dateStr,
                event.getDetailBean().getThumb());
        traceBean.id = FAR_AWAY_TIME - getTimeValue(timeStr);
        TraceDateBean traceDateBean = queryBuilder.build().findUnique();
        if (traceDateBean == null) {
            TraceDateBean dateBean = new TraceDateBean(dateStr);
            dateBean.id = FAR_AWAY_DATE - getDateValue(dateStr);
            dateBeanBox.put(dateBean);
            traceBeanBox.put(traceBean);
        } else {
            QueryBuilder<TraceBean> traceBeanQueryBuilder = traceBeanBox.query();
            traceBeanQueryBuilder.equal(TraceBean_.goodsId, traceBean.getGoodsId());
            traceBeanQueryBuilder.equal(TraceBean_.dateStr, traceDateBean.getDateStr());
            TraceBean oldTraceBean = traceBeanQueryBuilder.build().findUnique();
            if (oldTraceBean != null) {
                traceBeanBox.remove(oldTraceBean);
            }
            traceBeanBox.put(traceBean);
        }
    }

    private int getDateValue(String dateStr) {
        String[] dateStrs = dateStr.split("-");
        return Integer.valueOf(dateStrs[0]) * 10000 + Integer.valueOf(dateStrs[1]) * 100
                + Integer.valueOf(dateStrs[2]);
    }

    //2018 06 20 17 58 06
    private long getTimeValue(String timeStr) {
        long value = 0L;
        String[] timeStrs = timeStr.split("-");
        for (int i = 0; i < timeStrs.length; i++) {
            long baseValue = getBaseValue(timeStrs.length - 1 - i);
            value = value + Long.valueOf(timeStrs[i]) * baseValue;
        }
        LatteLogger.e("test", "timeValue = " + value);
        return value;
    }

    private long getBaseValue(int count) {
        long value = 1;
        for (int j = 0; j < count; j++) {
            value = value * 100;
        }
        return value;
    }

    private void changePage(int index, int id) {
        switch (index) {
            case 0: {
                GoodsIntroFragment delegate = new GoodsIntroFragment();
                Bundle bundle = new Bundle();
                bundle.putInt("id", id);
                bundle.putBoolean("scroll", true);
                delegate.setArguments(bundle);
                getSupportDelegate().replaceChildFragment(delegate, false);
            }
            break;
            case 1: {
                GoodsSpecsListFragment delegate = new GoodsSpecsListFragment();
                Bundle bundle = new Bundle();
                bundle.putInt("id", id);
                delegate.setArguments(bundle);
                getSupportDelegate().replaceChildFragment(delegate, false);
            }
            break;
            case 2: {
                GoodsPackageFragment delegate = new GoodsPackageFragment();
                Bundle bundle = new Bundle();
                bundle.putInt("id", id);
                delegate.setArguments(bundle);
                getSupportDelegate().replaceChildFragment(delegate, false);
            }
            break;
            case 3: {
                GoodsRecommendFragment delegate = new GoodsRecommendFragment().withMerchId(merchantId);
                Bundle bundle = new Bundle();
                bundle.putInt("id", id);
                delegate.setArguments(bundle);
                getSupportDelegate().replaceChildFragment(delegate, false);
            }
            break;
        }
    }

    private SelectParameterDialog dialog;
    private int showSpecType = 0;

    // type 0 : 加入购物车  1: 立即购买  2：选择商品规格/颜色
    private void showSpecDialog(int type) {
        showSpecType = type;
        if (dialog == null) {
            GoodsHandler.requestSpecsData(goodsId);
        } else {
            dialog.setType(type);
            dialog.show();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGotSpecsData(GotSpecsDataEvent event) {
        Log.e(TAG, "onGotData: ");
        dialog = new SelectParameterDialog(getContext(), event);
        showSpecDialog(showSpecType);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGoodsConfirm(ConfirmGoodsEvent event) {
        switch (event.getType()) {
            case 0: {//加入购物车
                //后端规定使用 商品详情界面的merchId
                event.getItemShopCart().setMerchId(2891);//mDetailBean.getMerchId()
                //上传用户选择的商品信息给后台
                GoodsHandler.addGoodsToCart(event.getItemShopCart());
            }
            break;
            case 1: {//立即购买
                //跳转到订单确认界面
                BillConfirmFragment fragment = new BillConfirmFragment().withType(2);
                Bundle bundle = new Bundle();
                bundle.putParcelable("item", event.getItemShopCart());
                fragment.setArguments(bundle);
                start(fragment);
            }
            break;
            case 2: {//选择属性
                specificationValueTv.setText(String.format(
                        getResources().getString(R.string.selected_format),
                        event.getItemShopCart().getOptionTitle()));
            }
            break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onAddToCartResult(AddGoodsToCartResult event) {
        if (event.getResult() == Flag.Result.SUCCEED) {
            Toast.makeText(getContext(), "商品已成功加入购物车", Toast.LENGTH_SHORT).show();
            ShopCartHandler.requestCartCount();
        } else {
            Toast.makeText(getContext(), "加入购物车失败", Toast.LENGTH_SHORT).show();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCollectResult(CollectGoodsResult result) {
        if (result.getResult() == Flag.Result.SUCCEED) {
            int resultType = result.getType();
            if (resultType == 1) {
                collectState = 1;
                collectImg.setImageDrawable(getResources().getDrawable(R.drawable.ic_star_red));
                Toast.makeText(_mActivity, "收藏成功", Toast.LENGTH_SHORT).show();
                collectTv.setTextColor(getResources().getColor(R.color.red));
            } else {
                collectState = 0;
                collectImg.setImageDrawable(getResources().getDrawable(R.drawable.ic_star_hollow));
                collectTv.setTextColor(getResources().getColor(R.color.black_light));
                Toast.makeText(_mActivity, "取消收藏成功", Toast.LENGTH_SHORT).show();
            }
        } else {
            String toast = result.getType() == 1 ? "收藏失败" : "取消收藏失败";
            Toast.makeText(_mActivity, toast, Toast.LENGTH_SHORT).show();
        }
    }

    private int collectState;

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGotCollectState(GotGoodsCollectState event) {
        if (event.getResult() == Flag.Result.SUCCEED) {
            if (event.getStatus() == 1) {
                collectState = 1;
                collectImg.setImageDrawable(getResources().getDrawable(R.drawable.ic_star_red));
                collectTv.setTextColor(getResources().getColor(R.color.red));
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onSubcateLoaded(SubCateLoaded event) {
        Log.e(TAG, "onSubcateLoaded: SubCateLoaded");
        setNestedScrollView(2);
    }
}